﻿namespace TestNewGuiFramework
{
    using System.Threading;

    using Pps.Gui;
    using Pps.Gui.Controls;

    internal abstract class BaseWindow : Window
    {
        private const int PERIOD = 3000;

        protected Button overview;
        protected Button temperature;
        protected Button heating;
        protected Button heatpump;

        private Timer DataUpdater;

        public BaseWindow()
        {
            this.DataUpdater = new Timer(new TimerCallback(BaseWindow.DataUpdate), this, Timeout.Infinite, Timeout.Infinite);

            this.Deactivated += this.WindowDeactivated;
            this.Activated += this.WindowActivated;
        }

        private void WindowActivated()
        {
            this.DataUpdater.Change(0, PERIOD);
        }

        private void WindowDeactivated()
        {
            this.DataUpdater.Change(Timeout.Infinite, Timeout.Infinite);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.DataUpdater.Dispose();
            }

            base.Dispose(disposing);
        }

        protected virtual void InitializeControls()
        {
            this.overview = new Button("Übersicht", 0, 0, 250, 120);
            this.overview.Text = "Übersicht";
            this.overview.Touched += Overview;
            this.overview.Selectable = true;
            this.overview.Selected = false;

            this.temperature = new Button("Temperatur", 0, 120, 250, 120);
            this.temperature.Touched += Temperature;
            this.temperature.Selectable = true;
            this.temperature.Selected = false;

            this.heating = new Button("Heizung", 0, 240, 250, 120);
            this.heating.Touched += Heating;
            this.heating.Selectable = true;
            this.heating.Selected = false;

            this.heatpump = new Button("Wärmepumpe", 0, 360, 250, 120);
            this.heatpump.Touched += Heatpump;
            this.heatpump.Selectable = true;
            this.heatpump.Selected = false;
        }

        protected virtual void AddControlsToWindow()
        {
            this.AddChildControl(this.overview);
            this.AddChildControl(this.temperature);
            this.AddChildControl(this.heating);
            this.AddChildControl(this.heatpump);
        }

        private static void DataUpdate(object sender)
        {
            BaseWindow window = sender as BaseWindow;

            if (window != null && window.IsActive() == true)
            {
                window.Update();
            }
        }

        protected virtual void Update()
        {

        }

        private void Overview()
        {
            WindowManager.ChangeToWindow(Program.Overview);
        }

        private void Temperature()
        {
            WindowManager.ChangeToWindow(Program.Temperature);
        }

        private void Heating()
        {
            WindowManager.ChangeToWindow(Program.Heating);
        }

        private void Heatpump()
        {
            WindowManager.ChangeToWindow(Program.Heatpump);
        }
    }
}